English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ map operator []Fonction utilisée pour utiliser une clé donnéeCléAccéder aux éléments de la map.
Il est similaireat()Fonction. La seule différence entre eux est que, si la clé visitée n'existe pas dans la map, une exception est lancée ; d'autre part, si la clé n'existe pas dans la map,operator[]Insérer la clé dans la map.
Considérez la clék,la syntaxe est :
mapped_type& operator[](const key_type& k); // à partir de C++ 11 Avant mapped_type& operator[](const key_type& k); //à partir de C++ 11 Commence mapped_type& operator[](key_type&& k); //à partir de C++ 11 Commence
k:La clé de l'élément de la map à accéder.
Il utilise les clés pour retourner une référence à la valeur de l'élément map.
Laissez-nous voir un exemple simple pour accéder aux éléments.
#include <iostream> #include <map> using namespace std; int main() { map<char, int> m = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, }; cout << "Les éléments de la map contiennent les éléments suivants" << endl; cout << "m['a'] = " << m['a'] << endl; cout << "m['b'] = " << m['b'] << endl; cout << "m['c'] = " << m['c'] << endl; cout << "m['d'] = " << m['d'] << endl; cout << "m['e'] = " << m['e'] << endl; return 0; }
Sortie :
La map contient les éléments suivants m['a'] = 1 m['b'] = 2 m['c'] = 3 m['d'] = 4 m['e'] = 5
Dans cet exemple, la fonction operator [] est utilisée pour accéder aux éléments de la map.
Laissez-nous voir un exemple simple pour ajouter des éléments en utilisant leurs clés.
#include <iostream> #include <string> #include <map> using namespace std; int main () { map<int,string> mymap = { { 101, "" }, { 102, "" }, { 103, ""} }; mymap[101] = "w3codebox"; mymap[102] = "."; mymap[103] = "com"; //Print the value associated with the key101The associated value, i.e., w}}3codebox cout << mymap[101]; // Print the value associated with the key102The associated value, i.e., . cout << mymap[102]; //Print the value associated with the key103The associated value, i.e., com cout << mymap[103]; return 0; }
Sortie :
oldtoolbag.com
In the above example, operator [] is used to add elements associated with the key value after initialization.
Let's look at a simple example to change the value associated with the key value.
#include <iostream> #include <string> #include <map> using namespace std; int main () { map<int,string> mymap = { { 100, "Nikita"}, { 200, "Deep" }, { 300, "Priya" }, { 400, "Suman" }, { 500, "Aman" }}; cout << "The element is:" << endl; for (auto& x: mymap) { cout << x.first << ": \ } mymap[100] = "Nidhi"; //The value associated with the key100 associated value is changed to Nidhi mymap[300] = "Pinku"; //The value associated with the key300 associated value is changed to Pinku mymap[500] = "Arohi"; //The value associated with the key500 associated value is changed to Arohi cout << "\nThe changed element is:" << endl; for (auto& x: mymap) { cout << x.first << ": \ } return 0; }
Sortie :
The element is: 100: Nikita 200: Deep 300: Priya 400: Suman 500: Aman The changed element is: 100: Nidhi 200: Deep 300: Pinku 400: Suman 500: Arohi
In the above example, the operator [] function is used to change the value associated with its key.
Let's look at a simple example to distinguish operator [] and at().
#include <iostream> #include <string> #include <map> using namespace std; int main () { map<char,string> mp = { { 'a',"Java"}, { 'b', "C++" }, { 'c', "Python" }}; cout << endl << mp['a']; cout << endl << mp['b']; cout << endl << mp['c']; mp['d'] = "SQL"; cout << endl << mp['d']; try { mp.at('z'); //Since there is no key with value 'z' in the map, an exception will be thrown catch(const out_of_range &e) { cout << endl << "\nException hors de portée " << e.what(); } return 0; }
Sortie :
Java C++ Python SQL Exception hors de portée map::at
Dans l'exemple ci-dessus, lorsque nous utilisons la fonction at(), elle lève une exception out_of_range, car il n'y a pas de valeur pour la clé z dans la carte, et lorsque nous utilisons operator [] pour ajouter un élément dans la clé d, sans clé dans la carte avec une valeur de "d", elle insère un couple clé-valeur avec la clé "d" et la valeur "SQL" dans la carte.